package com.bbx.flink.demo.ProcessFunctionAllDemo;

import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * 将流进行分割，将数值小于10 的进行侧输出
 */
public class SideOutProcessFunctionDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<Integer> process = env.socketTextStream("192.168.10.131", 10003)
                .map(i -> Integer.parseInt(i))
                .process(new SideOutProcessFunction());

        process.print("value1");
        process.getSideOutput(new OutputTag<Integer>("sideValue", TypeInformation.of(Integer.class))).print("value2");

        env.execute();



    }
}

class  SideOutProcessFunction extends ProcessFunction<Integer,Integer>{

    @Override
    public void processElement(Integer value, Context ctx, Collector<Integer> out) throws Exception {
        if (value>10) {
            out.collect(value);
        }else {
            ctx.output(new OutputTag<>("sideValue",TypeInformation.of(Integer.class)),value);
        }
    }
}